<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
            <el-form-item label="英文名称" prop="enName">
                <el-input v-model="queryParams.enName" placeholder="请输入英文名称" clearable
                          @keyup.enter="handleQuery"/>
            </el-form-item>
            <el-form-item label="中文名称" prop="zhName">
                <el-input v-model="queryParams.zhName" placeholder="请输入中文名称" clearable
                          @keyup.enter="handleQuery"/>
            </el-form-item>
            <el-form-item label="语言编码" prop="languageCode">
                <el-input v-model="queryParams.languageCode" placeholder="请输入语言编码" clearable
                          @keyup.enter="handleQuery"/>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['business:Language:add']">新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
                           v-hasPermi="['business:Language:edit']">修改
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
                           v-hasPermi="['business:Language:remove']">删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="Download" @click="handleExport"
                           v-hasPermi="['business:Language:export']">导出
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="LanguageList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="语言编号" align="center" prop="languageId"/>
            <el-table-column label="英文名称" align="center" prop="enName"/>
            <el-table-column label="中文名称" align="center" prop="zhName"/>
            <el-table-column label="排序值" align="center" prop="sortNum"/>
            <!--      <el-table-column label="创建时间" align="center" prop="createTime" width="180">-->
            <!--        <template #default="scope">-->
            <!--          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>-->
            <!--        </template>-->
            <!--      </el-table-column>-->
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                               v-hasPermi="['business:Language:edit']">修改
                    </el-button>
                    <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                               v-hasPermi="['business:Language:remove']">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                    v-model:limit="queryParams.pageSize"
                    @pagination="getList"/>

        <!-- 添加或修改翻译语种对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" append-to-body>
            <el-form ref="LanguageRef" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="英文名称" prop="enName">
                    <el-input v-model="form.enName" placeholder="请输入英文名称"/>
                </el-form-item>
                <el-form-item label="中文名称" prop="zhName">
                    <el-input v-model="form.zhName" placeholder="请输入中文名称"/>
                </el-form-item>
                <el-form-item label="语言编码" prop="languageCode">
                    <el-input v-model="form.languageCode" placeholder="请输入语言编码"/>
                </el-form-item>
                <el-form-item label="排序值" prop="sortNum">
                    <el-input v-model.number="form.sortNum" placeholder="请输入排序值"/>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup name="Language">
import {
    addLanguage,
    delLanguage,
    getLanguage,
    listLanguage,
    selectListLanguage,
    updateLanguage
} from "@/api/business/Language";
import {getCurrentInstance, reactive, ref, toRefs} from "vue";

const {proxy} = getCurrentInstance();

const LanguageList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const data = reactive({
    form: {},
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        enName: null,
        zhName: null,
        languageCode: null,
    },
    rules: {
        enName: [
            {required: true, message: "英文名称不能为空", trigger: "blur"}
        ],
        zhName: [
            {required: true, message: "中文名称不能为空", trigger: "blur"}
        ],
        languageCode: [
            {required: true, message: "语言编码不能为空", trigger: "blur"}
        ],
        delFlag: [
            {required: true, message: "是否删除 0否 1是不能为空", trigger: "blur"}
        ]
    }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询翻译语种列表 */
function getList() {
    loading.value = true;
    listLanguage(queryParams.value).then(response => {
        LanguageList.value = response.rows;
        total.value = response.total;
        loading.value = false;
    });
}

// 取消按钮
function cancel() {
    open.value = false;
    reset();
}

// 表单重置
function reset() {
    form.value = {
        languageId: null,
        enName: null,
        zhName: null,
        languageCode: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        sortNum: 0
    };
    proxy.resetForm("LanguageRef");
}

/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.pageNum = 1;
    getList();
}

/** 重置按钮操作 */
function resetQuery() {
    proxy.resetForm("queryRef");
    handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
    ids.value = selection.map(item => item.languageId);
    single.value = selection.length !== 1;
    multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
    reset();
    open.value = true;
    title.value = "添加翻译语种";
}

/** 修改按钮操作 */
function handleUpdate(row) {
    reset();
    const _languageId = row.languageId || ids.value
    getLanguage(_languageId).then(response => {
        form.value = response.data;
        open.value = true;
        title.value = "修改翻译语种";
    });
}

/** 提交按钮 */
function submitForm() {
    proxy.$refs["LanguageRef"].validate(valid => {
        if (valid) {
            if (form.value.languageId != null) {
                updateLanguage(form.value).then(response => {
                    proxy.$modal.msgSuccess("修改成功");
                    open.value = false;
                    getList();
                });
            } else {
                addLanguage(form.value).then(response => {
                    proxy.$modal.msgSuccess("新增成功");
                    open.value = false;
                    getList();
                });
            }
        }
    });
}

/** 删除按钮操作 */
function handleDelete(row) {
    const _languageIds = row.languageId || ids.value;
    proxy.$modal.confirm('是否确认删除翻译语种编号为"' + _languageIds + '"的数据项？').then(function () {
        return delLanguage(_languageIds);
    }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
    }).catch(() => {
    });
}

/** 导出按钮操作 */
function handleExport() {
    proxy.download('business/Language/export', {
        ...queryParams.value
    }, `Language_${new Date().getTime()}.xlsx`)
}

// 监听搜索条件的变化
watch(queryParams, (value, oldValue, onCleanup) => {
    getList();
}, {
    deep: true
})

getList();
</script>
